package com.bjshiwei.zhengu.ui.fragment;


import android.content.Intent;
import android.database.Cursor;
import android.support.v4.app.Fragment;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;

import com.bjshiwei.zhengu.R;
import com.bjshiwei.zhengu.adapter.IndividualMsgAdapter;
import com.bjshiwei.zhengu.bean.IndividualList;
import com.bjshiwei.zhengu.bean.IndividualMsgBean;
import com.bjshiwei.zhengu.config.NetConfig;
import com.bjshiwei.zhengu.requestInfo.RequestListenerInfo;
import com.bjshiwei.zhengu.utils.IndividualDao;
import com.bjshiwei.zhengu.utils.PrefUtil;
import com.bjshiwei.zhengu.utils.SerializationUtil;
import com.bjshiwei.zhengu.utils.VolleyRequest;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 */
public class IndividualMsgFragment extends BaseFragment {


    private PrefUtil mPrefUtil;
    private SerializationUtil mSerializationUtil;
    private List<IndividualList.Data> mDataList;
    private VolleyRequest mVolleyRequest;
    private PullToRefreshListView mListView;
    private List<IndividualMsgBean.DataBean> mDataBeen;
    private IndividualMsgAdapter mMsgAdapter;
    private Intent mIntent;
    private View emptyView;
    private IndividualDao mIndividualDao;
    private StringBuilder codeStr;
    private int loadNum = 0;
    private boolean isFirst;
    private boolean isUpDown;
    private int mCurrentPage;
    public static final int firstPage = 1;

    @Override
    protected void setListener() {
        /*mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mIntent = new Intent(getContext(), NoticeDetailActivity.class);
                if (!TextUtils.isEmpty(mDataBeen.get(position-1).getTxtContent())) {
                    String txtContent = mDataBeen.get(position-1).getTxtContent();
                    mIntent.putExtra("content", txtContent);
                }
                startActivity(mIntent);
            }
        });*/

       

        mListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {

            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                isUpDown = true;
                getData(firstPage);
                ILoadingLayout startLabels = mListView.getLoadingLayoutProxy(true, false);
                //设置下拉标签
                startLabels.setPullLabel("下拉刷新");
                //设置刷新标签
                startLabels.setRefreshingLabel("正在刷新");
                //设置释放标签
                startLabels.setReleaseLabel("释放开始刷新");

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                mCurrentPage++;
                isUpDown = false;
                ILoadingLayout startLabels = mListView.getLoadingLayoutProxy(false, true);
                //设置下拉标签
                startLabels.setPullLabel("加载更多");
                //设置刷新标签
                startLabels.setRefreshingLabel("正在加载");
                //设置释放标签
                startLabels.setReleaseLabel("释放开始加载");
                getData(mCurrentPage);
            }

        });


    }

    @Override
    protected void setData() {
        mListView.setAdapter(mMsgAdapter);
    }

    @Override
    protected void initData() {
        mDataBeen = new ArrayList<>();
        mDataList = new ArrayList<>();
        mMsgAdapter = new IndividualMsgAdapter(getContext(), mDataBeen);
        mPrefUtil = PrefUtil.getInstance(getContext());
        mSerializationUtil = SerializationUtil.getInstance(getContext());
        mVolleyRequest = VolleyRequest.getInstance(getContext());
    }


    public void getData(int currentPage) {
        codeStr = new StringBuilder();
        mIndividualDao = new IndividualDao(getContext());
        Cursor sw = mIndividualDao.findAll("sw", new String[]{"code"}, null, null);
        if (sw.getCount() > 0) {
            while (sw.moveToNext()) {
                String code = sw.getString(sw.getColumnIndex("code"));
                if (loadNum == 0) {
                    codeStr.append(code);
                } else {
                    codeStr.append(",");
                    codeStr.append(code);
                }
                loadNum++;
            }
        }

        Map<String, String> params = new HashMap<>();
        params.put("code", codeStr.toString());
        if(isUpDown) {
            params.put("pno", String.valueOf(mCurrentPage));
        }

        mVolleyRequest.jsonPoastRequest(NetConfig.INDIVIDUAL_MSG, params, new RequestListenerInfo(getActivity()) {
            @Override
            protected void onAnalysis(Object o, JSONObject jsonObject, boolean success) {
                if(isUpDown) {
                    mDataBeen.clear();
                }
                Gson gson = new Gson();
                IndividualMsgBean individualMsgBean = gson.fromJson(o.toString(), IndividualMsgBean.class);
                for (int i = 0; i < individualMsgBean.getData().size(); i++) {
                    mDataBeen.add(individualMsgBean.getData().get(i));
                }
                mMsgAdapter.notifyDataSetChanged();
                mListView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mListView.onRefreshComplete();
                    }
                }, 500);
            }
        });


    }

    @Override
    public void onResume() {
        super.onResume();
        getData(firstPage);
        mMsgAdapter.notifyDataSetChanged();
    }

    @Override
    protected View initView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.fragment_individual_msg, null);
    }

    @Override
    protected void initFindViewById(View view) {
        mListView = (PullToRefreshListView) view.findViewById(R.id.lv_individual_msg);
        emptyView = view.findViewById(R.id.tv_individual_msg_empty_view);
        mListView.setEmptyView(emptyView);

        mListView.setMode(PullToRefreshBase.Mode.BOTH);
        mListView.setPullToRefreshOverScrollEnabled(true);
        //设置下拉标签
        mListView.getLoadingLayoutProxy().setPullLabel("加载更多");
        //设置刷新标签
        mListView.getLoadingLayoutProxy().setRefreshingLabel("正在加载中...");
        //设置释放标签
        mListView.getLoadingLayoutProxy().setReleaseLabel("释放开始加载");

        String label = DateUtils.formatDateTime(getActivity(), System.currentTimeMillis(),
                DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
        mListView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
    }

}
